﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

namespace Manager
{
    public partial class EmailSetting : System.Web.UI.Page
    {
        string SMTPPassKey = Config.Global.Settings.SMTPPassKey;
        int SMTPPassIndex = Config.Global.Settings.SMTPPassIndex;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Cài đặt Email";
            if (!Page.IsPostBack)
            {
                lblMessage.Visible = false;
                Load_Data();
            }
        }

        private void Load_Data()
        {
            EmailSettingInfo emailInfo = EmailSettingData.SelectByEmailID(Config.Global.Settings.EmailID);

            txtEmailFrom.Text = emailInfo.EmailFrom;
            txtSMTPUsername.Text = emailInfo.SMTPUserName;
            txtSMTPPassword.Text = emailInfo.SMTPPassword;
            txtSMTPServer.Text = emailInfo.SMTPServer;
            txtSMTPPort.Text = emailInfo.SMTPPort.ToString();

            if (emailInfo.SMTPSSL)
            {
                rbtEnable.Checked = true;
            }
            else
            {
                rbtDisable.Checked = true;
            }

            if (emailInfo.EmailStatus == true)
            {
                rbtActive.Checked = true;
            }
            else
            {
                rbtDeactive.Checked = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string clearSMTPPass = txtSMTPPassword.Text.Trim();
            string cipherSMTPPass = Encrypt.SimpleEncryption.Encrypt(clearSMTPPass,SMTPPassKey);
            string messSMTPPass = Library.StringUtility.MessString(cipherSMTPPass, SMTPPassIndex);
            string email = txtEmailFrom.Text.Trim();

            if (!Library.Validate.IsEmail(email))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Email không đúng. Mời bạn nhập lại.";
                txtEmailFrom.Focus();
                return;
            }

            if (!Library.Validate.IsDigit(txtSMTPPort.Text.Trim()))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Cổng SMTP phải là một số nguyên. Mời bạn nhập lại.";
                txtSMTPPort.Focus();
                return;
            }

            EmailSettingInfo emailInfo = new EmailSettingInfo();

            emailInfo.EmailID = Config.Global.Settings.EmailID;
            emailInfo.SiteID = Config.Global.Settings.SiteID;
            emailInfo.EmailFrom = txtEmailFrom.Text.Trim();
            emailInfo.SMTPUserName = txtSMTPUsername.Text.Trim();
            emailInfo.SMTPPassword = messSMTPPass;
            emailInfo.SMTPServer = txtSMTPServer.Text.Trim();
            emailInfo.SMTPPort = Convert.ToInt32(txtSMTPPort.Text.Trim());

            if (rbtEnable.Checked)
            {
                emailInfo.SMTPSSL = true;
            } 
            else
            {
                emailInfo.SMTPSSL = false;
            }
            
            if (rbtActive.Checked)
            {
                emailInfo.EmailStatus = true;
            } 
            else
            {
                emailInfo.EmailStatus = false;
            }

            if (EmailSettingData.Update(emailInfo))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Cài đặt Email thành công!";
            } 
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Cài đặt Email không thành công!";
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}